class Solution {
public:
    int numberOfPairs(vector<vector<int>>& points) {
        sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b) {
            return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
            });

        int ret = 0, n = points.size();

        for (int i = 0; i < n; i++) {
            int y1 = points[i][1];
            int maxy = INT_MIN;
            for (int j = i + 1; j < n; j++) {
                int y2 = points[j][1];
                if (y2 <= y1 && y2 > maxy) {
                    ret++;
                    maxy = y2;
                }
            }
        }
        return ret;
    }
};